package com.lottery.service.pc28.accoutService.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lottery.dao.pc28.accoutDao.AutomaticCustomerDao;
import com.lottery.dto.ResultDto;
import com.lottery.entity.AutomaticCustomer;
import com.lottery.service.pc28.accoutService.AutomaticCustomerService;
import com.lottery.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AutomaticCustomerServiceimpl implements AutomaticCustomerService {

	@Autowired
	private AutomaticCustomerDao automaticcustomerDao;

	// 日志
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	// 自助客服问题录入
	@Override
	public String insertquestion(String question, String answer, int type) {
		JSONObject jObject = new JSONObject();
		try {
			// 创建一个对象
			AutomaticCustomer ac = new AutomaticCustomer();
			// 客服提前录入问题
			ac.setQuestion(question);
			ac.setAnswer(answer);
			ac.setQtype(type);
			automaticcustomerDao.insertAutomaticCustomer(ac);
			// 日志
			logger.info("ac:{}", ac);
			// 返回状态 ，成功为0
			jObject.put("result", "0");
			jObject.put("msg", "录入成功");
		} catch (Exception e) {
			e.printStackTrace();
			// 返回状态，失败为 1
			jObject.put("result", "1");
			jObject.put("Errmsg", "录入失败");

		}

		return jObject.toJSONString();
	}

	// 自助客服问题修改
	@Override
	public String updataquestion(int questionId, String question, String answer) {
		JSONObject jObject = new JSONObject();
		try {
			// 创建一个对象
			AutomaticCustomer ac = new AutomaticCustomer();
			// 客服修改问题及答案
			ac.setQuestionId(questionId);
			ac.setQuestion(question);
			ac.setAnswer(answer);
			automaticcustomerDao.updateAutomaticCustomer(ac);
			// 日志
			logger.info("ac:{}", ac);
			// 返回状态 ，成功为0
			jObject.put("result", "0");
			jObject.put("msg", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			// 返回状态，失败为 1
			jObject.put("result", "1");
			jObject.put("Errmsg", "修改失败");
		}
		// 日志
		logger.info("jObject:{}", jObject);
		return jObject.toJSONString();
	}

	// 自助客服问题删除
	@Override
	public String deletequestion(int questionId) {
		JSONObject jObject = new JSONObject();
		// 返回状态
		try {
			// 客服删除问题
			automaticcustomerDao.deleteAutomaticCustomer(questionId);
			// 返回成功，为0
			jObject.put("result", "0");
			jObject.put("msg", "删除成功");

		} catch (Exception e) {
			e.printStackTrace();
			// 返回状态，失败为 1
			jObject.put("result", "1");
			jObject.put("Errmsg", "删除失败");
		}
		// 日志
		logger.info("jObject:{}", jObject);
		return jObject.toJSONString();
	}

	// 自助客服问题请求应答
	@Override
	public String question(int questionId) {
		JSONObject jObject = new JSONObject();
		// 返回状态
		try {
			// 用户请求应答是根据用户输入的问题的id，来查询出答案
			AutomaticCustomer ac = automaticcustomerDao.selectByquestionId(questionId);
			// 返回成功为0
			jObject.put("result", "0");
			jObject.put("answer", ac.getAnswer());
			jObject.put("msg", "请求成功");
		} catch (Exception e) {
			e.printStackTrace();
			// 返回 失败 为1
			jObject.put("result", "1");
			jObject.put("Errmsg", "请求失败");
		}
		// 日志
		logger.info("jObject:{}", jObject);
		return jObject.toJSONString();
	}

	// 自动 拉取 问题目录
	@Override
	public String getquestions() {
		// 日志
		logger.info("getquestions in:{}", "进来了");
		// 通过查询，拉取问题目录
		List<AutomaticCustomer> selectByquestion = automaticcustomerDao.selectByquestion();
		Map<String, Object> maxmap = new HashMap<String, Object>();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		// 返回 状态
		try {
			// Date date = new Date();
			// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd
			// HH:mm:ss");
			// String dataNow = sdf.format(date);
			String dataNow = DateUtil.getFormatDate("yyyy-MM-dd HH:mm:ss");
			int index = 0;

			for (AutomaticCustomer one : selectByquestion) {
				// if (one.getQtype() == 0) {
				Map<String, Object> map = new HashMap<String, Object>();
				List<String> answerList = new ArrayList<>();
				if (one.getQtype() == 1) {
					String[] answer = one.getAnswer().split("&&&");
					for (String string : answer) {
						answerList.add(string);
					}
				} else {
					answerList.add(one.getAnswer());
				}
				map.put("type", one.getQtype());
				map.put("question", one.getQuestion());
				// map.put("answer", one.getAnswer());
				map.put("answer", answerList);
				map.put("index", index++);
				list.add(map);
				// break;
				// }
			}
			// for (AutomaticCustomer one : selectByquestion) {
			// map = new HashMap<String, Object>();
			// map.put("type", 1);
			// // map.put("question_id", one.getQuestionId());
			// map.put("question", one.getQuestion());
			// map.put("answer", one.getAnswer());
			// map.put("index", index++);
			// list.add(map);
			// }
			//
			// for (AutomaticCustomer one : selectByquestion) {
			// if (one.getQtype() == 2) {
			// map.put("type", 2);
			// map.put("question", one.getQuestion());
			// map.put("index", index++);
			// map.put("answer", "");
			// list.add(map);
			// break;
			// }
			// }

			// 发送成功，返回状态0,条数和Arraymsg数组
			maxmap.put("result", "0");
			maxmap.put("msgTime", dataNow);
			maxmap.put("array", list);
			// 日志
			logger.info("maxmap:{}", maxmap);
		} catch (

		Exception e) {
			e.printStackTrace();
			// 发送给失败，返回 状态 1，失败原因
			maxmap.put("result", "1");
			maxmap.put("msg", "拉取失败");
		}
		// 转成JsontoString
		String json = JSON.toJSONString(maxmap, true);
		return json;
	}

	/**
	 * 机器人欢迎语接口
	 */
	@Override
	public ResultDto<AutomaticCustomer> insertwelcome(String welcome, String rote) {
		ResultDto<AutomaticCustomer> result = new ResultDto<AutomaticCustomer>();
		try {
			AutomaticCustomer automaticwelcome = new AutomaticCustomer();
			automaticcustomerDao.deleteType(0);
			automaticwelcome.setQuestion(welcome);
			automaticwelcome.setQtype(0);
			automaticcustomerDao.insertAutomaticCustomer(automaticwelcome);

			AutomaticCustomer automaticrote = new AutomaticCustomer();
			automaticrote.setQuestion(rote);
			automaticrote.setQtype(2);
			automaticcustomerDao.deleteType(2);
			automaticcustomerDao.insertAutomaticCustomer(automaticrote);
			result.setSuccess(true);
			result.setMessage("添加成功");

		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage("添加失败");
		}

		return result;

	}

	/**
	 * 机器人设置语显示
	 */
	@Override
	public ResultDto<AutomaticCustomer> selectByType(AutomaticCustomer automaticCustomer) {
		ResultDto<AutomaticCustomer> result = new ResultDto<AutomaticCustomer>();
		try {
			automaticCustomer = automaticcustomerDao.selectByType(automaticCustomer);
			// String welcome = selectByType.getQuestion();
			// String rote =
			result.setData(automaticCustomer);
			result.setSuccess(true);
			result.setMessage("查询成功");
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage("查询失败");
		}
		return result;
	}

}
